package algorithm.middle;

import algorithm.easy.FindComplement476;
import pojo.TreeNode;

import java.util.ArrayList;
import java.util.List;

public class MaxLevelSum1161 {

    int resIndex = 1;
    int sum = 0;

    public int maxLevelSum(TreeNode root) {
        sum = root.val;
        resIndex = 1;
        ArrayList<TreeNode> first = new ArrayList<>();
        first.add(root);
        fun(first, resIndex);
        return resIndex;
    }

    public void fun(List<TreeNode> list, int cur) {
        if (list.isEmpty()) return;
        ArrayList<TreeNode> nextList = new ArrayList<>();
        int count = 0;
        for (int i = 0; i < list.size(); i++) {
            TreeNode treeNode = list.get(i);
            count += treeNode.val;
            if (treeNode.left != null) {
                nextList.add(treeNode.left);
            }
            if (treeNode.right != null) {
                nextList.add(treeNode.right);
            }
        }
        if (count > sum) {
            resIndex = cur;
            sum = count;
        }
        cur++;
        fun(nextList, cur);
    }

}
